//
//  JZHMessgeViewController.m
//  极致荟
//
//  Created by hetao on 14-5-7.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHMessgeViewController.h"
#import "JZHMessgeTableViewCell.h"
#import "JZHPersonaldetailsViewController.h"
@interface JZHMessgeViewController ()
{
    NSString *strImageHead;
    
    NSMutableArray *m_arrayList;
}
@property (weak, nonatomic) IBOutlet UITableView *m_tableView;

@end

@implementation JZHMessgeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    m_arrayList = [NSMutableArray array];
    
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
    [leftButton addTarget:self action:@selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    [titleLabel setTextColor:[UIColor blackColor]];
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    titleLabel.font = [UIFont boldSystemFontOfSize:16];
    titleLabel.text =@"消息";
    self.navigationItem.titleView= titleLabel;
    if(OSVersionIsAtLeastiOS7>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets=NO;
    }

    [self httpLoadData];
}

- (void)leftDrawerButtonPress:(UIButton *)but
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark-------TableView代理----------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

{
    return m_arrayList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *pengyoucell=@"PengyouCell";
    
    JZHMessgeTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:pengyoucell];
    
    if(!cell)
    {
        cell=[[[NSBundle mainBundle] loadNibNamed:@"JZHMessgeTableViewCell" owner:self options:nil] objectAtIndex:0];
    }
    
    NSDictionary *dict = [m_arrayList objectAtIndex:indexPath.row];
    
    [ImageCache imageUrl:[NSString stringWithFormat:@"%@%@", strImageHead, [dict objectForKey:@"userAvatar"]] UiImageview:cell.imgIcon];
    
    [cell.lblNickName setText:[dict objectForKey:@"userNickName"]];
    
    if ([[dict objectForKey:@"listType"] isEqualToString:@"comment"])
    {
        [cell.lblContent setHidden:NO];
        [cell.lblContent setText:[dict objectForKey:@"content"]];
        
        [cell.btnZan setHidden:YES];
    }
    
    NSDate *date=[NSDate dateWithTimeIntervalSince1970:[[dict objectForKey:@"addTime"] doubleValue]];
    NSDateFormatter *datafomatter=[NSDateFormatter new];
    datafomatter.dateFormat=@"yyyy年MM月dd HH:mm";
    NSString *dataStr=[datafomatter stringFromDate:date];
    NSString *strTime =[NSString stringWithFormat:@"%@", dataStr];
    [cell.lblTime setText:strTime];
    
    [ImageCache imageUrl:[NSString stringWithFormat:@"%@%@", strImageHead, [dict objectForKey:@"dynamicImg"]] UiImageview:cell.imgDynamicImage];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    NSDictionary *dict = [m_arrayList objectAtIndex:indexPath.row];
//    
//    User *user = [[User alloc]init];
//    user.userId = [[dict objectForKey:@"id"] intValue];
//    user.name = [dict objectForKey:@"userNickName"];
//    
//    [self.navigationController pushViewController:[JZHPersonaldetailsViewController shareName:user] animated:YES];
}

- (void)httpLoadData
{
    /*
     userId：用户ID(需登录)
     relatedType：相关类型(这里和上面点赞与评论的类型写一致，最好是friends_dynamic)
     */
    
    //[MMProgressHUD showWithStatus:@"数据加载中..."];
    NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:[User defuceUser].userId], @"userId", @"friend_dynamic", @"relatedType", nil];
    [HttpRequest httpRequestPost:JZHHTTPURL(@"friendsDynamic/dynamicMessageList") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        if(rerurncode==0)
        {
            //[MMProgressHUD dismissWithSuccess:@"获取成功"];
            strImageHead = [[data objectForKey:@"data"] objectForKey:@"imgPrefix"];
            
            m_arrayList = [NSMutableArray arrayWithArray:[[data objectForKey:@"data"] objectForKey:@"list"]];
            [self.m_tableView reloadData];
        }
        else
        {
            [MMProgressHUD  showInformationWithTips:@"获取失败"];
        }
        
    }];
}

@end
